class Solution {
public:
    bool CheckCol[10];
    bool CheckDig1[20];
    bool CheckDig2[20];
    vector<vector<string>> ret;
    vector<string> path;
    int n;

    vector<vector<string>> solveNQueens(int _n) {
        n = _n;
        path.resize(n);
        for (int i = 0; i < n; i++)
            path[i].append(n, '.');
        dfs(0);
        return ret;
    }

    void dfs(int row)
    {
        if (row == n)
        {
            ret.push_back(path);
            return;
        }

        for (int col = 0; col < n; col++)
        {
            if (!CheckCol[col] && !CheckDig1[row - col + n] && !CheckDig2[row + col])
            {
                path[row][col] = 'Q';
                CheckCol[col] = CheckDig1[row - col + n] = CheckDig2[row + col] = true;
                dfs(row + 1);
                path[row][col] = '.';
                CheckCol[col] = CheckDig1[row - col + n] = CheckDig2[row + col] = false;
            }
        }
    }
};